//
//  ViewFactory.m
//  CarCare
//
//  Created by ileo on 14-8-30.
//  Copyright (c) 2014年 baozun. All rights reserved.
//

#import "ViewFactory.h"
#import "CButton.h"
#import "CodeButton.h"
#import "EyeButton.h"
#import "ScreenMgr.h"
#import "FLabel.h"

@implementation ViewFactory

+(UIButton *)buttonWithFrame:(CGRect)frame title:(NSString *)title normalColor:(UIColor *)normal highlightColor:(UIColor *)highlight disabledColor:(UIColor *)disabled inView:(UIView *)view action:(void (^)())action{
    
    CButton *button = [[CButton alloc] initWithFrame:frame title:title normalColor:normal highlightColor:highlight disabledColor:disabled action:action];
    button.layer.cornerRadius = 2;
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [view addSubview:button];
    
    return button;
}

+(UIButton *)normalButtonWithFrame:(CGRect)frame title:(NSString *)title inView:(UIView *)view action:(void (^)())action{
    CButton *button = [[CButton alloc] initWithFrame:frame title:title normalColor:[UIColor colorWithHexString:@"#1F90E5"] highlightColor:[UIColor colorWithHexString:@"#1F90E5"] disabledColor:[UIColor colorWithHexString:@"#1F90E5"] action:action];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.layer.cornerRadius = 2;
    [view addSubview:button];
    return button;
}

+(UIButton *)emptyButtonWithFrame:(CGRect)frame title:(NSString *)title inView:(UIView *)view action:(void (^)())action{
    CButton *button = [[CButton alloc] initWithFrame:frame title:title normalColor:[UIColor clearColor] highlightColor:[UIColor colorWithHexString:@"#39c8e3"] disabledColor:[UIColor colorWithHexString:@"#919191"] action:action];
    [button setTitleColor:[Theme mainColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    button.layer.cornerRadius = 6;
    button.layer.borderColor = [UIColor colorWithHexString:@"#39c8e3"].CGColor;
    button.layer.borderWidth = 1;
    [view addSubview:button];
    return button;
}

+(UITextField *)textFieldWithFrame:(CGRect)frame type:(TEXTFIELD_TYPE)type hasBG:(BOOL)bg inView:(UIView *)view{
    UITextField *textField = [[UITextField alloc] initWithFrame:frame];
    if (type == TEXTFIELD_NORMAL) {
        return textField;
    }
    textField.textAlignment = NSTextAlignmentLeft;
    textField.textColor = [UIColor blackColor];
    textField.font = FONT_DEFAULT_Light(16);
    textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    
    // textFiled背景修改（tianxing）
    [[[UIView viewWithFrame:CGRectMake(0, CGRectGetMinY(frame), __MAIN_WIDTH, CGRectGetHeight(frame))] resetBackgroundColor:[UIColor whiteColor]] setupOnView:view];
//    bgV.layer.borderWidth = 0.5;
//    bgV.layer.borderColor = [Theme lineColor].CGColor;
    UILabel *tipL = [Factory labelWithFrame:CGRectMake(22, CGRectGetMinY(frame), 70, CGRectGetHeight(frame)) font:FONT_DEFAULT_Light(16) text:@"" textColor:COLOR_HEXSTRING(@"#181818") onView:view textAlignment:NSTextAlignmentLeft];
    textField.left += 80;
    textField.width -= 80;
    [view addSubview:textField];
    
    switch (type) {
        case TEXTFIELD_TEL:
            textField.width -= 6;
            tipL.text = @"手机号";
            textField.clearButtonMode = UITextFieldViewModeWhileEditing;
            textField.keyboardType = UIKeyboardTypeNumberPad;
            textField.placeholder = LOCSTRING(@"fill_tel");
            break;
            
        case TEXTFIELD_PWD:
            tipL.text = @"密码";
            textField.font = [UIFont systemFontOfSize:15];
            textField.placeholder = LOCSTRING(@"fill_pwd");
            textField.returnKeyType = UIReturnKeyDone;
            textField.secureTextEntry = YES;
            break;
            
        case TEXTFIELD_CODE:
            tipL.text = @"验证码";
            textField.keyboardType = UIKeyboardTypeNumberPad;
            textField.placeholder = LOCSTRING(@"fill_code");
            break;
            
        case TEXTFIELD_Ali:
            tipL.text = @"支付宝";
            textField.placeholder = LOCSTRING(@"fill_ali");
            break;
            
        case TEXTFIELD_Name:
            tipL.text = @"用户名";
            textField.placeholder = LOCSTRING(@"fill_name");
            break;
            
        case TEXTFIELD_Money:
            
            break;
            
        case TEXTFIELD_REALName:
            tipL.text = @"姓名";
            textField.placeholder = @"请输入姓名";
            break;
            
        case TEXTFIELD_IDCARD:
            tipL.text = @"身份证";
            textField.placeholder = @"请输入身份证";
            break;
        case TEXTFIELD_ENGINE:
            tipL.text = @"发动机号";
            textField.keyboardType = UIKeyboardTypeASCIICapable;
            textField.left += 10;
            textField.placeholder = @"请输入发动机号";
            break;
        case TEXTFIELD_VIN:
            tipL.text = @"车架号";
            textField.keyboardType = UIKeyboardTypeASCIICapable;
            textField.placeholder = @"请输入车架号";
            break;
            
        case TEXTFIELD_Default_Num:
            textField.keyboardType = UIKeyboardTypeNumberPad;
            textField.left -= 35;
            textField.width += 35;
            break;
        
            
        default:
            break;
    }
    
    return textField;
}

+(UIButton *)addButtonType:(BUTTON_TYPE)type onTextField:(UITextField *)textField inView:(UIView *)view action:(void (^)())action ok:(void (^)())ok{
    
    switch (type) {
        case BUTTON_CODE:
        {
            CodeButton *but = [[CodeButton alloc] initWithFrame:CODE_RECT action:action ok:ok];
            but.center = CGPointMake(0, textField.centerY);
            but.right = __MAIN_WIDTH - (__MAIN_WIDTH - (324*__MAIN_RATIO_375))/2;
            [view addSubview:but];
            return but;
            break;
        }
        case BUTTON_EYE:
        {
            EyeButton *but = [[EyeButton alloc] initWithTextField:textField];
            [view addSubview:but];
            return but;
        }
            break;
        case BUTTON_Money:
        {
            [Factory imageViewWithCenter:CGPointMake(textField.right - 20, textField.centerY) image:ImageNamed(@"go.png") onView:view];
            UIButton *button = [[UIButton buttonEmptyWithFrame:textField.frame click:action] setupOnView:view];
            return button;
        }
            break;
        case BUTTON_TEL:
        {
            UIButton *button = [Factory buttonWithFrame:CGRectMake(0, 0, 50, 50) withImageName:@"address_book" click:action onView:view];
            button.center = CGPointMake(textField.right - 20, textField.centerY);
            return button;
        }
            break;
        case BUTTON_DEVICE_CODE:
        {
            UIButton *button = [Factory buttonWithFrame:CGRectMake(0, 0, 50, 50) withImageName:@"devicecode" click:action onView:view];
            button.center = CGPointMake(textField.right - 20, textField.centerY);
            return button;
        }
        default:
            break;
    }
    return nil;
}

+(UILabel *)addTextLabel:(NSString *)text onTextField:(UITextField *)textField inView:(UIView *)view{

    UILabel *label = [Factory labelWithFrame:CGRectMake(0, 0, view.width/2, 40) font:[UIFont systemFontOfSize:14] text:text textColor:[UIColor lightGrayColor] onView:nil textAlignment:NSTextAlignmentRight];
    label.center = textField.center;
    label.right = textField.right - 35;
    [view insertSubview:label belowSubview:textField];
    return label;
}

+(UILabel *)labelWithMaxWidth:(CGFloat)maxWidth resetSizeFinish:(void (^)(UILabel*))finish text:(NSString *)text inView:(UIView *)view{
    FLabel *label = [[FLabel alloc] initWithMaxWidth:maxWidth resetSizeFinish:finish];
    label.backgroundColor = [UIColor clearColor];
    label.text = text;
    [view addSubview:label];
    return label;
}


+(TipsView *)factoryCreateVisiterTips{
    
    TipsView *tips = [TipsView textTipsWithTitle:nil lightTitles:@[LOCSTRING(@"tip_visiter_limit")]];
    [tips addLeftButton:LOCSTRING(@"cancel") clickLeft:^{
        [AlertMgr dismiss:TYPE_VIEW_ALERT];
    } rightButton:LOCSTRING(@"ok") clickRight:^{
        [AlertMgr dismiss:TYPE_VIEW_ALERT];
        [SCREEN changeToLoginScreen];
    }];
    [AlertMgr showView:tips];
    return tips;
}

@end
